package com.common方法;

import java.util.Map;
import java.util.TreeMap;

/**
 * @author liyiruo
 * @Description
 * @Date 2024/4/22 下午12:06
 */
public class SortMain {
    public static void main(String[] args) {

        int[][] aa = {{99, 2, 3}, {3, 5}, {4, 6}};
        System.out.println("------------排序前-----------------");
        printArr(aa);
        int[][] ints = sortBySomeColum(aa, 1);
        System.out.println("--------排序后----------------");
        printArr(ints);
    }

    /**
     *
     * @param aa 一个二位数组
     * @param n  数组中的第几列
     * @return
     */
    public static int[][] sortBySomeColum(int[][] aa, int n) {
        //如果某行数据长度不到n怎么办？不考虑
        int[][] isnull = new int[aa.length][];
        int[][] notnull = new int[aa.length][];
        Map<Integer, Object> sortedMap = new TreeMap();
        int aalength = aa.length;
        for (int i = 0; i < aalength; i++) {
            int[] row = aa[i];
            int length = row.length;
            if (length < n) {
                // isnull
                System.out.println("长度小于n的行，不参与排序");
            } else {
                sortedMap.put(row[n - 1], row);
            }
        }
        int index = 0;
        for (Map.Entry<Integer, Object> entry : sortedMap.entrySet()) {
            int[] row = (int[]) entry.getValue();
            notnull[index] = row;
            index++;
        }
        System.out.println("某列排序后----->"+sortedMap.keySet().toString());
        return notnull;
    }
    public static void printArr(int[][] ints) {
        for (int i = 0; i < ints.length; i++) {
            int[] anInt = ints[i];
            if (anInt==null){
                continue;
            }
            for (int j = 0; j < anInt.length; j++) {
                System.out.println(ints[i][j]);
            }
        }

    }
}
